package com.nf.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.nf.dao.StudentDao;
import com.nf.dao.StudentsDaoImpl;
import com.nf.entity.Students;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/AllStudents")
public class AllStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("All来了");
        //设置编码
        response.setContentType("application/json;charset=utf-8");
        //调用dao操作数据库查询
        StudentDao dao = new StudentsDaoImpl();
        List<Students> stuList = dao.queryAll();
        //使用jackson将对象转换为json打印回客户端
        ObjectMapper objectMapper = new ObjectMapper();
        //定义一个模板设置日期格式
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

        response.getWriter().print(objectMapper.writeValueAsString(stuList));
    }
}
